prec <- function(y, date, first = date[1], last = date[length(date)],
                 plotID, col, main, ylab_line, ylab_histo, 
                 type = c("line", "hist", "both"), ...) 
{
  
################################################################################
##  
##  This program plots a time series variable either as a time line, a histogram
##  or both. It is possible to pass further standard lattice arguments. 
##  
##  parameters are as follows:
##  y = object to be plotted (no default)
##  date = the date variable from the input time series dataframe (no default)
##  first = starting date of the plot (format: "%Y-%m-%d %H:%M:%S")
##          (default is first of date)
##  last = same as first for closing date
##  plotID = conditioning variable (optional - no default)
##  type = plot type
##         character string: one of "line" (time line), "hist" (histogram), "both"
##
##  no default settings
##
################################################################################
##
##  Copyright (C) 2011 Steffi Instinsky, Simon Lange, Melanie Schnelle
##
##  This program is free software: you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation, either version 3 of the License, or
##  (at your option) any later version.
##
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
##  GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License
##  along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##  Please send any comments, suggestions, criticism, or (for our sake) bug
##  reports to tim.appelhans@gmail.com
##
################################################################################                        
  
  ## convert date variable and 'first' and 'last' parameter to POSIX
  date <- as.POSIXct(date)
  first <- as.POSIXct(first)
  last <- as.POSIXct(last)
  
  ## define plot object
  lineplot <- xyplot(y ~ date | plotID, col = col, ylab = ylab_line, type = "l",
              subset = date > first & date < last & plotID != "",
              layout = c(1,2), xlim = c(first,last), ...)

  ## define and combine histograms
  histomai1 <- histogram(~ y | plotID, freq = T, col = col, ylab = ylab_histo, 
                         subset = plotID == "mai1", xlab = "mai1")
  
  histocof3 <- histogram(~ y | plotID, freq = T, col = col, ylab = ylab_histo,
                         subset = plotID == "cof3", xlab = "cof3")
  
  combohisto <- c(histomai1, histocof3, layout = c(2,1))
  
  
  ## define combination plot object
  both <- c(lineplot, combohisto, layout = c(2, 2))
  
  ## define out plot object conditional on what type is passed from function call
  out <- switch(type,
                "line" = lineplot,
                "hist" = combohisto,
                "both" = both)
  
   return(print(out))

}
## Example
data <- read.table("C:/Users/Melanie/Desktop/RKurs/Precipitation/dataset_precipitation.dat", 
                   fill = T, sep = ",", header = T)

## load necessary packages
library(latticeExtra)


#prec(data$precip_wxt, data$dateEAT, plotID = data$plotID, type = "both")

prec(y = data$precip_wxt, 
            date = data$dateEAT, 
            first = "2011-04-01 00:00:00",
            last = "2011-04-30 23:55:00", 
            plotID = data$plotID,
            col = "blue",
            main = "Precipitation",
            ylab_line = "Precipitation[mm]",
            ylab_histo = "Distribution",
            type = "both")
 